<?php
require_once 'prepend.php';

require_once 'PHPTAL.php';
require_once 'MTranslator.php';

$skin = 'default';
#$skin = 'bitter_sweet';

// execute the template
try {
	$tr = new MTranslator();
	// set language to use for this session (first valid language will
	// be used)
	if(!$_GET['lang'] || $_GET['lang'] == 'en')
		$tr->setLanguage('en', 'en_GB.utf8', 'en_GB');
	else
		$tr->setLanguage('pt_BR.utf8');
	
	// create a new template object
	#$tpl = new PHPTAL($_SERVER['DOCUMENT_ROOT'].MConfig::$obCfg->path->skins_path.'/default/default.tpl');
	$tpl = new PHPTAL($_SERVER['DOCUMENT_ROOT'].MConfig::$obCfg->path->skins_path.'/'.$skin.'/mtranslator.tpl');
	
	// tell PHPTAL to use our translator
    $tpl->setTranslator($tr);
    
    /*.................. SET VARS*/
    $mmenu = MConfig::getMMenu();
    //var_dump($mmenu);
	//foreach($mmenu as $k => $v){echo "<br>$k => $v<br>";}
    $tpl->menuOptions = $mmenu;
    $tpl->lang = !$_GET['lang'] ? 'en' : $_GET['lang'];
    $tpl->action = !array_key_exists('action',$_GET) ? false : $_GET['action'];
    
    switch ($_GET['action']) {
    	case 'list':
    		$tpl->edit_list = false;
    		$tpl->content = $tr->getList(1);
    		
	    	if($_GET['edit']):
	    		$tpl->edit = $_GET['edit'];
	    		$tpl->edit_list = 'Edit item [' . $_GET['edit'] . ']';
	    		$tpl->content =  false;	
	    		$tpl->update = false;
	    		$tpl->editRes = $tr->editListResult($_GET['edit']);
	    	elseif($_GET['delet']):
	    		$tpl->content =  'Romve ' . $_GET['delet'];
	    	endif;
	    	
	   		if(array_key_exists('update', $_GET)){
	   			//echo "am i here in update?<br>";
	   			$tpl->update = true;
	   			$tpl->content = false;
	   			$tpl->editRes = $tr->editTranslation($_GET['edit'], $_GET['edit_list']);
	   		}
    	break;
    	
    	case 'add_lang':
    	$tpl->content = null;
    	
    	case 'add_domain':
    	$tpl->content = null;
    	break;
    	
    	case 'add_translation':
    		$tpl->content = null;
    		$tpl->ok_msn = null;
    		$tpl->error_msn=null;
    		//get available domains
    		$domains = $tr->getDomains();
    		if(!$_GET['submit']):
    		
    		if(null !== $domains){
    			//prepare the option tags
    			$res = '';
    			foreach ($domains as $domain){
    				$res .= '<option value="'.$domain['id'].'">'.$domain['domain_name'].'</option>';
    			}
    		} else {$res = null;}
    		//#################################################3
    		//Set form variables
    		$tpl->domainSelectionOptions = $res;
    		
    		else:
    		//Getting here(local validation) if the form was submitted
    		$checkout = checkout($_GET);
    		$tpl->error_msn= $checkout;
    		
    		if(null !== $domains){
    			//prepare tags with selected parameter
    			$res = '';
    			foreach ($domains as $domain){
    				if($_GET['select_domain'] == $domain['id'])
    					$res .= '<option value="'.$domain['id'].'" selected="selected">'.$domain['domain_name'].'</option>';
    				else
    					$res .= '<option value="'.$domain['id'].'">'.$domain['domain_name'].'</option>';
    			}
    		} else {$res = null;}
    		
    		//Set form variables
    		$tpl->domainSelectionOptions = $res;
    		$tpl->msgid = $_GET['add_msgid'];
    		$tpl->msgstr = $_GET['add_msgstr'];
    		
    		//start add procedure
    		if(!$checkout){
    			$tr->setDebug(true);
    			if($tr->addTranslation($_GET['add_msgid'], $_GET['add_msgstr'], $_GET['select_domain'])):
    			$tpl->ok_msn = "Translation added!";
    			else:
    			$tpl->error_msn="Error: The translation was NOT added!<br/>".$tr->error[2];
    			endif;
    		}
    		endif;
    	break;
    }
    
    
    /*.................. END VARS*/

	echo $tpl->execute();
}
catch (Exception $e){
	//header('location:http://localhost/moreia/merror.php?msg='.$e->getMessage());
	echo $e;
}


//mtranslator specific validation
function checkout($request){
	
	$res = "";
	if('null' === $request['select_domain'])
		$res .= "Please select a domain.<br/>";

	if(!$request['add_msgid'])
		$res .= "Type a valid translation ID!<br/>";
	
	if(!$request['add_msgstr'])
		$res .= "Type the translation.<br/>";
		
	return $res;
}
?>